import csv
import xlwt
from selenium.webdriver.common.by import By
from selenium import webdriver

house_list = []
pages = range(1, 40)
city = input('输入城市：')
cities = {'北京': 'zu', '上海': 'sh.zu', '广州': 'gz.zu', '深圳': 'sz.zu', '天津': 'tj.zu', '成都': 'cd.zu'}
index0 = 0
wb1 = xlwt.Workbook()
sheet = wb1.add_sheet('data1')
titles = (['title', 'name', 'way', 'layout', 'size', 'zone', 'position', 'location', 'money', 'subway'])
for index, title in enumerate(titles):
    sheet.write(0, index, title)
# 2.打开浏览器
for i in pages:
    wb = webdriver.Chrome('C:\Program Files\Google\Chrome\Application\chromedriver.exe')
    url = f'https://{cities[city]}.fang.com/house/i3{i}/?rfss=1-1fcccd379d8b38bd19-b6'
    wb.get(url)
    element_list = wb.find_elements(By.XPATH, '//*[@id="listBox"]/div[3]/dl')

    for element in element_list:
        title = element.find_element(By.XPATH, './dd/p[1]/a').get_attribute('title')
        name = element.find_element(By.XPATH, './dd/p[3]/a[3]/span').text
        layout = element.find_element(By.XPATH, './dd/p[2]').text.split('|')[1]
        position = element.find_element(By.XPATH, './dd/p[3]/a[2]/span').text
        size = element.find_element(By.XPATH, './dd/p[2]').text.split('|')[2]
        zone = element.find_element(By.XPATH, './dd/p[3]/a[1]/span').text
        way = element.find_element(By.XPATH, './dd/p[2]').text.split('|')[0]
        money = element.find_element(By.XPATH, './dd/div[2]/p/span').text
        try:
            location = element.find_element(By.XPATH, './dd/p[2]').text.split('|')[3]
        except:
            location = '无'
        try:
            subway = element.find_element(By.XPATH, './dd/div/p/span/a').text
            location = element.find_element(By.XPATH, './dd/p[2]').text.split('|')[3]
        except:
            subway = '无'
        list_temp = [title, name, way, layout, size, zone, position, location, money, subway]
        # house_list.append(list_temp)

        # 存入excel文件
        for j, val in enumerate(list_temp):
            sheet.write(index0 + 1, j, val)

        index0 = index0 + 1

# write = csv.writer(open('house_data.csv', 'w', encoding='utf-8'))
# write.writerow(['name', 'way', 'layout', 'size', 'zone', 'position', 'location', 'money'])
# write.writerows(house_list)

wb1.save('data_house2.xls')
